(function(){
	var gulp = require('gulp'),
		usemin = require('gulp-usemin'),
        connect = require('gulp-connect'),
        open = require('gulp-open'),
        mockServer = require('gulp-mock-server'),
        rename = require('gulp-rename'),
        path = require('path'),
        minifyJs = require('gulp-uglify'),
        sourcemaps = require('gulp-sourcemaps'),
        minifyCSS = require('gulp-cssnano'),
        concat = require('gulp-concat'),
        paths = {
        	root: './',
        	data: '.',
        	build: {
        		root: 'build/',
                styles: 'build/css/',
                scripts: 'build/js/'
        	},
        	source: {
                root: 'src/',
                styles: 'src/css/*.*',
                scripts: 'src/js/*.js',
                images: 'src/**/*.jpg|gif|png|svg',
                html:'src/**/*.html'
            },
        };

	    /* ==================================================================
	    Build 
	    ================================================================== */
	    // Build Styles and Scripts
	    gulp.task('usemin', function() {
		    return gulp.src(paths.source.root+'index.html')
		        .pipe(usemin({
		            js: [minifyJs(), 'concat'],
		            css: [minifyCSS({keepSpecialComments: 0}), 'concat'],
		        }))
		        .pipe(gulp.dest(paths.build.root))
		});

		gulp.task('images', function() {
		    return gulp.src(paths.source.images)
		        .pipe(gulp.dest(paths.build.root))
		        .pipe(connect.reload());
		});

		// gulp.task('scripts', function() {
		//     return gulp.src(paths.source.scripts)
		//         .pipe(minifyJs())
		//         .pipe(concat('app.js'))
		//         .pipe(gulp.dest(paths.build.scripts))
		//         .pipe(connect.reload());;
		// });
		gulp.task('scripts', function() {
		    return gulp.src(paths.source.scripts)
		        .pipe(concat('app.js'))
		        .pipe(gulp.dest(paths.build.scripts))
		        .pipe(connect.reload());;
		});

		gulp.task('styles', function(){
		    return gulp.src(paths.source.styles)
		        .pipe(minifyCSS())
		        .pipe(concat('app.css'))
		        .pipe(gulp.dest(paths.build.styles))
		        .pipe(connect.reload());;
		});

		gulp.task('html',function(){
			return gulp.src([paths.source.html,'!'+paths.source.root + 'index.html'])
		        .pipe(gulp.dest(paths.build.root))
		        .pipe(connect.reload());
		})

	    /* =================================
	    Watch
	    ================================= */
	    gulp.task('watch', function () {
	        gulp.watch(paths.source.images, [ 'images' ]);
	        gulp.watch(paths.source.scripts, [ 'scripts' ]);
	        gulp.watch(paths.source.styles, [ 'styles' ]);
			gulp.watch(paths.source.html, [ 'html' ]);
	    });

	    gulp.task('connect', function () {
	        return connect.server({
	            root: [ paths.build.root ],
	            livereload: true,
	            port:3300
	        });
	    });

	    
	    gulp.task('open', function () {
	        return gulp.src('./index.html').pipe(open({ uri: 'http://localhost:3300/index.html'}));
	    });

	    gulp.task('mock',function(){
	    	gulp.src(paths.data)
	    		.pipe(mockServer({
	    			port:8090
	    		}))
	    })

	    gulp.task('server', [ 'watch', 'connect', 'open','mock','build']);

	    gulp.task('build',['usemin', 'images', 'styles','scripts','html'])

	    gulp.task('default', [ 'server']);
	    
})();